<?php

App::uses('AppHelper', 'View/Helper');
App::import('Model', 'Post');

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       app.View.Helper
 */
class PostsHelper extends AppHelper {

    public $helpers = array('Html');

    /**
     * Show images
     */
    public function thumbSmall($post) {
        if (empty($post['Post']['image'])) {
            return $this->Html->image("/img/No-Image-Available.gif", array('alt' => $post['Post']['image'], 'title' => $post['Post']['title'], 'class' => 'post-small'));
        }
        return $this->Html->image("/" . str_replace('\\', '/', $post['Post']['dir']) . '/thumb/small/' . $post['Post']['image'], array('alt' => $post['Post']['image'], 'title' => $post['Post']['title'], 'class' => 'post-small'));
    }

    public function thumbMedium($post) {
        if (empty($post['Post']['image'])) {
            return $this->Html->image("/img/No-Image-Available.gif", array('alt' => $post['Post']['image'], 'title' => $post['Post']['title'], 'class' => 'post-medium'));
        }
        return $this->Html->image("/" . str_replace('\\', '/', $post['Post']['dir']) . '/thumb/medium/' . $post['Post']['image'], array('alt' => $post['Post']['image'], 'title' => $post['Post']['title'], 'class' => 'post-medium'));
    }

    public function thumbBig($post) {
        if (empty($post['Post']['image'])) {
            return $this->Html->image("/img/No-Image-Available.gif", array('alt' => $post['Post']['image'], 'title' => $post['Post']['title'], 'class' => 'post-big'));
        }
        return $this->Html->image("/" . str_replace('\\', '/', $post['Post']['dir']) . '/thumb/big/' . $post['Post']['image'], array('alt' => $post['Post']['image'], 'title' => $post['Post']['title'], 'class' => 'post-big'));
    }

    /**
     * show status
     */
    public function showStatus($id, $status) {
        $url = $this->Html->url(array('controller' => 'posts', 'action' => 'admin_updateStatus'));
        if ($status == Post::publish) {
            return "<div class='publish-status' title='" . __('Click to draft') . "' onclick=updateStatus('" . $url . "'," . $id . "," . Post::draf . ")>" . __("Posted") . "</div>";
        }
        if ($status == Post::draf) {
            return "<div class='draf-status' title='" . __("Click to Post") . "' onclick=updateStatus('" . $url . "'," . $id . "," . Post::publish . ")>" . __("Drafting") . "</div>";
        }
    }

    /**
     * getSeoLink
     */
    public function getSEOLink($post) {
        $Category = new Category();
        $realPath = $Category->getPath($post['Post']['category_id'], array('id', 'slug'), -1);
        $url = "/";
        if ($realPath) {
            foreach ($realPath as $p) {
                $url .= $p['Category']['slug'] . "/";
            }
        }
        $url .= $post['Post']['slug'];
        return $url;
    }

    /**
     * getPostsByCategoryId
     *
     */
    public function getPostsByCategoryId($categoryId) {
        $Post = new Post();
        $posts = $Post->find('all', array('conditions' =>
            array('category_id' => $categoryId), 'fields' => array('Post.id', 'Post.slug', 'Post.updated_at')));
        return $posts;
    }

}
